package com.zzh.myapplication.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.ToolBarHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/3/18.
 */
public class RemainingSumActivity extends BaseActivity {
    private Toolbar toolbar;
    private TextView tv_dong_money;
    private TextView tv_ya_money;
    private TextView tv_yu_money;
    private RelativeLayout rl_remainsum_recharge;
    private RelativeLayout rl_deposit_record;
    private RelativeLayout rl_applyfor_deposit;
    private RelativeLayout rl_pay_setting;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_remainingsum);
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ToolBarHelper.setNormalTitleAndBack(this, toolbar, getString(R.string.remainingsum));
        tv_ya_money = (TextView) findViewById(R.id.tv_ya_money);
        tv_dong_money = (TextView) findViewById(R.id.tv_dong_money);
        tv_yu_money = (TextView) findViewById(R.id.tv_yu_money);
        rl_remainsum_recharge = (RelativeLayout) findViewById(R.id.rl_remainsum_recharge);
        rl_deposit_record = (RelativeLayout) findViewById(R.id.rl_deposit_record);
        rl_applyfor_deposit = (RelativeLayout) findViewById(R.id.rl_applyfor_deposit);
        rl_pay_setting = (RelativeLayout) findViewById(R.id.rl_pay_setting);

        GetBalanceofAccount();
    }

    public void GetBalanceofAccount() {
        JSONObject o = new JSONObject();
        try {
            o.put("mobile", SPUtils.getString(RemainingSumActivity.this, "LoginInfo", ""));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        PublicRequest.customMainRequestPost(this, UrlValues.QUERY_ACCOUNT_WALLET, handler, o);
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 2000:
                    try {
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray array = object.getJSONArray("assetsList");
                        System.out.println("array::" + array.toString());
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object1 = array.optJSONObject(i);
                            System.out.println("object::" + object1.toString());
                            String assetsType = object1.optString("assetsType");
                            if (assetsType.equals("01")) {
                                tv_yu_money.setText(object1.optString("assetsValue"));
                            }
                            if (assetsType.equals("05")) {
                                tv_dong_money.setText(object1.optString("assetsValue"));
                            }
                            if (assetsType.equals("06")) {
                                tv_ya_money.setText(object1.optString("assetsValue"));
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    public void setListener() {

        rl_remainsum_recharge.setOnClickListener(this);
        rl_deposit_record.setOnClickListener(this);
        rl_applyfor_deposit.setOnClickListener(this);
        rl_pay_setting.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.rl_remainsum_recharge:
                intent.setClass(RemainingSumActivity.this, RemainingSumRechargeActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_deposit_record:
                intent.setClass(RemainingSumActivity.this, DepostitRecordActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_applyfor_deposit:
                intent.setClass(RemainingSumActivity.this, ApplyForDepositActivity.class);
                start_Activity(intent);
                break;
            case R.id.rl_pay_setting:
                intent.setClass(RemainingSumActivity.this, ChangePayPasswdActivity.class);
                start_Activity(intent);
                break;

        }
    }
}
